import Vue from 'vue'
import App from './App.vue'
import router from './router'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import axios from "axios"
Vue.prototype.$axios = axios

// axios.defaults.withCredentials = true;
axios.defaults.baseURL = 'http://127.0.0.1:8088/webserver'

const CancelToken = axios.CancelToken
const defaultConfig = {}
const pending = []
axios.interceptors.response.use((response) => {
  const data = response.data
  if (data.code === 401) {
    while (pending.length > 0) {
      pending.pop()
    }
    router.replace({
      path: '/login',
      query: { redirect: router.currentRoute.fullPath }
    })
    Vue.prototype.$message.error('重新登录')
    localStorage.removeItem('token')
  }
  return response
}, (error) => {
  return Promise.reject(error)
})

defaultConfig.cancelToken = new CancelToken((c) => {
  pending.push(c)
})

axios(defaultConfig)

Vue.config.productionTip = false
Vue.use(ElementUI);
new Vue({
  router,
  render: h => h(App)
}).$mount('#app')
